<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{  

    //主页面
    public function index($tid=-1,$leixing='ALL')
    { 
      $type=model('type')->order('orderT desc')->paginate(8);
      if($tid==-1&&$leixing=='ALL'){
         $message=model('message')->getAll(0);
      }elseif($tid!=-1&&$leixing=='ALL'){
         $message=model('message')->getAllByType($tid);
      }elseif($tid!=-1&&$leixing!='ALL'){
         $message=Db::name('message')
              ->where(['tid'=>$tid,'type'=>$leixing])
              ->paginate(8);
      }else{
        $message=Db::name('message')
              ->where('type',$leixing)
              ->paginate(8);
      }
      return view('',[
        'type'=>$type,
        'message'=>$message,
        'title'=>'帮帮网主页',
        'tid'=>$tid,
        'leixing'=>$leixing
      ]);
    }

    //条件搜索
    public function search()
    {
       $str0=input('search');
       $str='';
       foreach (mb_str_split($str0) as $i)
       {   
          $str.='%'.$i;
       }
       $str=$str.'%';
       $message=model('message')->getAllBy($str); 
       $type=model('type')->order('orderT desc')->paginate(8); 
       return view('index',[
       'type'=>$type,
       'message'=>$message,
       'title'=>'帮帮网主页',
       'tid'=>-1,
       'leixing'=>'All',
       'search'=>$str0
      ]);
    }

  
    

    //登陆页面
    public function login()
    {
      return view();
    }
    
    //ajax检查用户名是否存在
    public function checkUser($val){
      $result=Db::name('user')->where('uname',$val)->find();
      if($result){
        return 1;
      }else{
        return 0;
      }
    }

    //ajax检查email是否存在
    public function checkEmail($val){
      $result=Db::name('user')->where('email',$val)->find();
      if($result){
        return 1;
      }else{
        return 0;
      }
    }


    //ajax检查验证码是否正确
    public function checkCaptcha($val){
      if(captcha_check($val)){
        return 1;
      }else{
        return 0;
      }
    }

    //检查登陆
    public function check()
    {
        $data=input('post.'); 
        // halt($data);  
        $search=Db::name('user')
               ->where('uname',$data['uname'])
               ->find();
        if($search['pwd']==md5($search['code'].$data['pwd'])){
            // $search['exp']=Db::name('exp')->where('uid',$search['id'])->select();
            cookie('user',$search);
            // halt($search);
            switch ($search['status']) {
              case '0':
                return $this->success('管理员登陆成功，自动为您跳转后台','admin/index/index');
                break;
              case '1':
                return $this->redirect('index/index/index');
                break;         
              default:
                return $this->success('你是什么用户呢,出错了','index/index/index');
                break;
              }
        }else{
            return $this->error('密码错误，请核对',url('index/login'));
        }
    }    
     
  
     //普通用户注册页面
     public function register()
     {
        if(!request()->isPost()){
          return view();
        }else{
          $data=input('post.');
          $file=request()->file('image');
          $image = \think\Image::open($file);
          $num=time();
          $image=$image->thumb(410, 410)->save("./uploads/person/{$num}.png");
          $data['image']="/person/{$num}.png";
          $validate=validate('user');
          if($validate->check($data)){
            unset($data['repwd']);
            $data['code']=rand(1000,9999);
            $data['pwd']=md5($data['code'].$data['pwd']);
            $result=model('user')->save($data);
            if($result){
                return $this->success('注册成功，去登陆','login');
            }else{
                return $this->error('注册失败，请重试！');
            }
          }else{
              return $this->error($validate->getError());
          }
         }
     }
    

    //退出登陆
    public function loginout()
    {
      cookie('user',null);
      return $this->redirect('index');
    }

}